<template>
   <!-- 操作栏 -->
   <div class="action-bar">
        <el-button
          type="primary"
          icon="plus"
          @click="showShareEdit"
        >
          写文章
        </el-button>
      </div>
    <Table
        ref="tableInfoRef"
        :columns="columns"
        :fetch="loadDataList"
        :dataSource="tableData"
        :options="tableOptions"
        :extHeight="tableOptions.extHeight"
        :selected="selectedHandler"
        @rowSelected="rowSelected"
      >
        <template #slotCover="{ index, row }">
          <Cover
            v-if="row.coverType == 0"
            title="无封面"
            bgColor="#ddd"
            :width="100"
            :height="100"
          ></Cover>
          <Cover
            v-if="row.coverType == 1"
            :width="200"
            :height="100"
            :cover="row.coverPath"
          ></Cover>
          <Cover
            v-if="row.coverType == 2"
            :width="100"
            :height="100"
            :cover="row.coverPath"
          ></Cover>
        </template>

        <template #slotTitle="{ index, row }">
          <span class="a-link" >{{
            row.title
          }}</span>
        </template>

        <template #slotStatus="{ index, row }">
          <Badge showType="orange" text="待发布" v-if="row.status == 0"></Badge>
          <Badge showType="green" text="已发布" v-if="row.status == 1"></Badge>
        </template>

        <template #slotOperation="{ index, row }">
          <div class="row-op-panel">
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="showShareEdit(row)"
              v-if="row.status == 0"
              >修改</a
            >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="delInfo(row)"
              >删除</a
            >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="updateStatus(row,'1')"
              v-if="row.status == 0"
              >发布</a
            >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="updateStatus(row,'0')"
              v-if="row.status == 1"
              >取消发布</a
            >
          </div>
        </template>
      </Table>
      <ShareEdit ref="shareEditRef" @reload="loadDataList"></ShareEdit>
  </template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick } from "vue"
const { proxy } = getCurrentInstance();
import ShareEdit from "../post/ShareEdit.vue";

const api={
  loadDataList:"/post/loadPostsList4User",
  delShare: "/post/delPost",
  updateStatus: "/post/updateStatus",
}

const tableData = ref({});
const tableOptions = ref({
  selectType: "checkbox",
  extHeight: "105",
});

const columns = [
  {
    label: "ID",
    prop: "postId",
    width: 80,
  },
  {
    label: "封面",
    prop: "cover",
    scopedSlots: "slotCover",
    width: 220,
  },
  {
    label: "标题",
    prop: "title",
    scopedSlots: "slotTitle",
  },
  {
    label: "创建人",
    prop: "username",
    width: 150,
  },
  {
    label: "创建时间",
    prop: "createTime",
    width: 180,
  },
  {
    label: "发布状态",
    prop: "status",
    scopedSlots: "slotStatus",
    width: 90,
  },
  {
    label: "操作",
    width: 130,
    scopedSlots: "slotOperation",
  },
];

const  shareEditRef=ref();
const showShareEdit=(data)=>{
  shareEditRef.value.showEdit(data);
}




const searchForm=ref({});
const tableInfoRef = ref();

const loadDataList = async () => {
  let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
  };
  Object.assign(params, searchForm.value);
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  });
  if (!result) {
    return;
  }
  Object.assign(tableData.value, result.data);
};



//删除
const delInfo = (data) => {
  proxy.Confirm(`确定要删除【${data.title}】吗？`, async () => {
    delShareInfo({ id: data.postId },
    api.delShare);
  });
};


const delShareInfo = async (params, url) => {
  let result = await proxy.Request({
    url: url,
    params: params,
  });
  if (!result) {
    return;
  }
  proxy.Message.success("删除成功");
  loadDataList();
};


const updateStatus= async(data,status)=>{
  let info=status==0?'取消发布':"发布";
  proxy.Confirm(`确定要【${info}】【${data.title}】吗？`, async () => {
    let result = await proxy.Request({
    url: api.updateStatus,
    params: {
      status:status,
      id:data.postId,
    },
  });
  if (!result) {
    return;
  }
  proxy.Message.success("操作成功");
  loadDataList();
  });
}
</script>

<style lang="scss" scoped>
.action-bar {
      margin-bottom: 20px;
      padding: 15px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    }
</style>
